﻿using System;
using MCP.Model.Repositories.CF;
using MCP.Model.Repositories.Interfaces;

namespace MCP.Model.Repositories
{
	public class UnitOfWork : IUnitOfWork
	{
		private readonly MCPContext context;

		public UnitOfWork(MCPContext context
			//,
			//IAccountRepository accountRepository,
			//ICategoryRepository categoryRepository,
			//ICheckRepository checkRepository
			)
		{
			this.context = context;
			this.context.Database.CreateIfNotExists();

			this.AccountRepository = new AccountRepository(this, context);
			this.CategoryRepository = new CategoryRepository(context);
			this.CheckRepository = new CheckRepository(this, context);
		}

		#region Repositories

        //private ICheckRepository checkRepository;
        //private ICategoryRepository categoryRepository;
        //private ISettingsRepository settingsRepository;
        //private IShopRepository shopRepository;
        //private IUserRepository userRepository;

        public IAccountRepository AccountRepository { get; set; }

        public ICheckRepository CheckRepository { get; set; }
        //{
        //    get
        //    {
        //        if (this.checkRepository == null)
        //        {
        //            this.checkRepository = new CheckRepository(this, context);
        //        }

        //        return this.checkRepository;
        //    }
        //}

        public ICategoryRepository CategoryRepository { get; set; }
        //{
        //    get
        //    {
        //        if (this.categoryRepository == null)
        //        {
        //            this.categoryRepository = new CategoryRepository(context);
        //        }

        //        return this.categoryRepository;
        //    }
        //}

        public ISettingsRepository SettingsRepository { get; set; }
        //{
        //    get
        //    {
        //        if (this.settingsRepository == null)
        //        {
        //            this.settingsRepository = new SettingsRepository(context);
        //        }

        //        return this.settingsRepository;
        //    }
        //}

        public IShopRepository ShopRepository { get; set; }
        //{
        //    get
        //    {
        //        if (this.shopRepository == null)
        //        {
        //            this.shopRepository = new ShopRepository(context);
        //        }

        //        return this.shopRepository;
        //    }
        //}

        //public IUserRepository UserRepository { get; set; }
        //{
        //    get
        //    {
        //        if (this.userRepository == null)
        //        {
        //            this.userRepository = new UserRepository(context);
        //        }

        //        return this.userRepository;
        //    }
        //}

		#endregion

		/// <summary>
		/// Saves context changes.
		/// </summary>
		public void Save()
		{
			this.context.SaveChanges();
		}

		#region IDisposable

		private bool disposed = false;

		protected virtual void Dispose(bool disposing)
		{
			if (!this.disposed)
			{
				if (disposing)
				{
					context.Dispose();
				}
			}
			this.disposed = true;
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		#endregion
    }
}